<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible"
    width="80%">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="120px">
    <el-form-item label="职员名" prop="name">
      <el-input v-model="dataForm.name" placeholder="职员名"></el-input>
    </el-form-item>
    <el-form-item label="职员编号" prop="code">
      <el-input v-model="dataForm.code" placeholder="职员编号"></el-input>
    </el-form-item>
    <el-form-item label="角色ID" prop="roleId">
      <el-input v-model="dataForm.roleId" placeholder="角色ID"></el-input>
    </el-form-item>
    <el-form-item label="身份证号" prop="idCard">
      <el-input v-model="dataForm.idCard" placeholder="身份证号"></el-input>
    </el-form-item>
    <el-form-item label="公司" prop="salary">
      <el-input v-model="dataForm.salary" placeholder="公司"></el-input>
    </el-form-item>
    <el-form-item label="登录名" prop="loginName">
      <el-input v-model="dataForm.loginName" placeholder="登录名"></el-input>
    </el-form-item>
      <el-form-item label="备注" prop="remark">
        <el-input v-model="dataForm.remark" placeholder="备注"></el-input>
      </el-form-item>
<!--    <el-form-item label="是否可登录，0否1是" prop="canLog">-->
<!--      <el-input v-model="dataForm.canLog" placeholder="是否可登录，0否1是"></el-input>-->
<!--    </el-form-item>-->

<!--      <el-form-item label="是否可登录" size="mini" prop="canLog">-->
<!--        <el-radio-group v-model="dataForm.canLog">-->
<!--          <el-radio :label="0">否</el-radio>-->
<!--          <el-radio :label="1">是</el-radio>-->
<!--        </el-radio-group>-->
<!--      </el-form-item>-->

    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          id: 0,
          name: '',
          code: '',
          roleId: '',
          idCard: '',
          salary: '',
          loginName: '',
          canLog: 1,
          remark: '',
          status: ''
        },
        dataRule: {
          name: [
            { required: true, message: '职员名不能为空', trigger: 'blur' }
          ],
          code: [
            { required: true, message: '职员编号不能为空', trigger: 'blur' }
          ],
          // roleId: [
          //   { required: true, message: '角色ID不能为空', trigger: 'blur' }
          // ],
          // idCard: [
          //   { required: true, message: '身份证号不能为空', trigger: 'blur' }
          // ],
          // salary: [
          //   { required: true, message: '公司不能为空', trigger: 'blur' }
          // ],
          loginName: [
            { required: true, message: '登录名不能为空', trigger: 'blur' }
          ]
          // ,
          // canLog: [
          //   { required: true, message: '是否可登录，0否1是不能为空', trigger: 'blur' }
          // ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.id = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.id) {
            this.$http({
              url: this.$http.adornUrl(`/sto/staff/info/${this.dataForm.id}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.name = data.staff.name
                this.dataForm.code = data.staff.code
                this.dataForm.roleId = data.staff.roleId
                this.dataForm.idCard = data.staff.idCard
                this.dataForm.salary = data.staff.salary
                this.dataForm.loginName = data.staff.loginName
                this.dataForm.canLog = data.staff.canLog
                this.dataForm.remark = data.staff.remark
                this.dataForm.status = data.staff.status
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/sto/staff/${!this.dataForm.id ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'id': this.dataForm.id || undefined,
                'name': this.dataForm.name,
                'code': this.dataForm.code,
                'roleId': this.dataForm.roleId,
                'idCard': this.dataForm.idCard,
                'salary': this.dataForm.salary,
                'loginName': this.dataForm.loginName,
                'canLog': this.dataForm.canLog,
                'remark': this.dataForm.remark,
                'status': this.dataForm.status
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
